﻿package{
	import gs.TweenLite;
	//use component button
	import com.bit101.components.PushButton;
	import com.bit101.components.Label;
	import flash.display.Shape;
	import flash.geom.Matrix;
	import flash.display.Graphics;
	import flash.display.GradientType;
	import flash.text.TextField;
    import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	//set flash
	//set line3D
	import org.papervision3d.materials.special.LineMaterial;
	import org.papervision3d.core.geom.Lines3D;
	import org.papervision3d.core.geom.renderables.Vertex3D;
	import org.papervision3d.core.geom.renderables.Line3D;
	  
	import flash.system.System;
	import flash.display.StageAlign;
	import flash.display.StageQuality;
	import flash.display.StageScaleMode;
	import flash.display.Sprite;
	import flash.display.MovieClip;
	import flash.display.Stage;
	import flash.filters.GlowFilter;
	import flash.geom.Point;
	import dropdownmenu.*;
	import flash.events.*;	
	//set timer
	import flash.events.Event; 
    import flash.utils.Timer; 
	//import qemlive.hedge.HEMesh;
	//import qemlive.TeleQEM;
	import org.papervision3d.materials.WireframeMaterial;
	import org.papervision3d.events.FileLoadEvent;
	import flash.utils.ByteArray;
	import org.papervision3d.materials.MovieMaterial;
 
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.view.BasicView;
	import org.papervision3d.cameras.Camera3D;
	import org.papervision3d.materials.WireframeMaterial;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.objects.primitives.Sphere;
	import org.papervision3d.render.BasicRenderEngine;
	import org.papervision3d.scenes.Scene3D;
	import org.papervision3d.objects.parsers.DAE;
	import org.papervision3d.view.Viewport3D;
	import org.papervision3d.view.layer.ViewportLayer;
	import org.papervision3d.materials.utils.MaterialsList;
	import org.papervision3d.materials.BitmapMaterial;
	import org.papervision3d.objects.parsers.Collada;
	import org.papervision3d.materials.BitmapFileMaterial;
	import org.papervision3d.objects.primitives.Sphere;
	import org.papervision3d.objects.parsers.KMZ;
	
	//[SWF (width="1200", height="600", backgroundColor="000000", frameRate="40")] 
	public class Main extends Sprite{
		//cusstom vp for main
		public static var customVp:CustomVp = new CustomVp();
		//custom main
		private var newShape3:Shape;
		public static var containerM:Sprite= new Sprite();
		public static var bg3:Shape;
		public static var bgCustom:Shape;
		public var isMouseDown : Boolean = false; 
		public var targetPitch : Number = 0; 
		public var targetYaw : Number = 0;
		public var pitch : Number = 0; 
		public var yaw : Number = 0; 
		public var curMousePoint : Point = new Point();
		//set DAE tmp for main viewport
		private var object    :DAE;
		//set text Warning:
		public var textWarnning:String;
		public static var txt:TextField=new TextField();
 		private static var txtFormat:TextFormat=new TextFormat();
		//set main viewport for custom
		private var mainCustom:Papervision;
		
		//set pushbutton component
		private var materialButton:PushButton;
		private var customButton:PushButton;
		private var noteButton:PushButton;
		private var addChildModel:PushButton;
		private var help:PushButton;
		//set button menu main
		private var rotationButton:PushButton;
		private var zoomButton:PushButton;
		private var note2Button:PushButton;
		//private var addChildModel:PushButton;
		private var custom2Button:PushButton;
		//set for label
		private var topLabel:Label;
		
		//set down button
		private var fWheelButton:PushButton;
		private var bWheelButton:PushButton;
		private var bodyButton:PushButton;
		//rearview mirrorS
		private var rMirrorsButton:PushButton;
		//car accessory
		private var accessoryButton:PushButton;
		//set for down label
		private var bottomLabel:Label;
		//set 2 button cancel and ok
		private var cancelButton:PushButton;
		private var okButton:PushButton;
		//set container for customvp
		public static var _customvp:Sprite = new Sprite();
		private var _container:Sprite = new Sprite();
		//set scene.
		private var kmz:KMZ;
		//private var model1:HEMesh;
		private var leftViewport:Viewport3D;
		private var leftCamera:Camera3D;
		private var rightViewport:Viewport3D;
		private var rightCamera:Camera3D;
		private var scene:Scene3D;
		private var sceneMain:Scene3D;
		private var renderer:BasicRenderEngine;
		private var mainViewport:Viewport3D;
		private var centerCamera:Camera3D;
		
		//set main scene
		private var viewport:Viewport3D;
		private var camera:Camera3D;
		
		
		private var model:DisplayObject3D;
		//set material
		
		protected var materialList:MaterialsList;
		protected var materialList2:MaterialsList;
		protected var materialList3:MaterialsList;
		protected var bitmapFileMaterial:BitmapFileMaterial;
		
		public var memoryBefore:uint = System.totalMemory;
		//set timer properties
		public var minuteTimer:Timer = new Timer(2000);
		
		private var d1:DropDownMenu=new DropDownMenu("xml/menu.xml",120,30,"Size");
		private var d2:DropDownMenu=new DropDownMenu(null,120,30,"Firm");
		private var d3:DropDownMenu=new DropDownMenu(null,123,30,"Name");
		public static var d4:DropDownMenu=new DropDownMenu(null,120);
		public static var stagex:int =210;
		public static var stagey:int=150;
		//set properties main stage
		private var toolBoxes:DisplayObject3D;
		private var car:DisplayObject3D
		private var ground:DisplayObject3D;
		private var wheels:DisplayObject3D;
		private var workLight :DisplayObject3D;
		private var workLight2 :DisplayObject3D;
		private var workLightFloor:DisplayObject3D;
		private var workLightFloor2:DisplayObject3D;
		public var sphere:Sphere;
		
		//set document_page
		private var Document_page:BitmapViewport;
		private var doc:DocumentPage = new DocumentPage("1.SELECT DATA");
		
		
		//set document for document 2
		private var doc2:DocumentPage = new DocumentPage("2.DOCUMENT");
		var ms:ButtonTrace= new ButtonTrace();
		
		//* set actionhanlder for customvp
		public static var valueChange:Boolean = false;
		
		////set embed 4 class for scene center.
//	[Embed(source="unBLANK_Nascar_Shadow.dae", mimeType="application/octet-stream")] private var shadowDAE:Class;
//   
//	
//	  [Embed(source="unBLANK_Nascar_toolBoxes.dae", mimeType="application/octet-stream")] private var boxDAE:Class;
//  
//	
//	  [Embed(source="unBLANK_Nascar_wheels.dae", mimeType="application/octet-stream")] private var wheelDAE:Class;
//   //set material
//     [Embed(source="garageDoor.jpg", mimeType="application/octet-stream")] private var DoorBitmap:Class;
//	 
//	   [Embed(source="inGroundLight.jpg", mimeType="application/octet-stream")] private var lightBitmap:Class;
//	ve 1 doi tuong hcn
		private function drawObject(_width:int,_height:int,colorF:String,colorS:String):Shape{
			var newShape=new Shape();
			var colours=[colorF,colorS];
			var alphas=[1,1];
			var ratios=[0,255];
			var radians:Number=90*(Math.PI/180);
			var matrix:Matrix=new Matrix();
			matrix.createGradientBox(_width,_height,radians);
			newShape.graphics.beginGradientFill(GradientType.LINEAR,colours,alphas,ratios,matrix);
			newShape.graphics.drawRoundRect(0,0,_width/1.5,_height/1.2,5,5);
			return newShape;
		}
		
		public function Main():void{
			//stage.align = StageAlign.TOP_LEFT;
			stage.quality = StageQuality.HIGH;
			stage.scaleMode = StageScaleMode.SHOW_ALL;
			//stage.displayState = StageDisplayState.FULL_SCREEN;
			camera= new Camera3D();
			camera.fov = 45; 
			viewport= new Viewport3D(800,500);
			viewport.x=stage.stageWidth-850;
			viewport.y=stage.stageHeight-550;
			viewport.interactive=true;
            viewport.buttonMode = true;
			//addChild(viewport);
			viewport.filters = [new GlowFilter(0xcc0000, 1, 10, 10, 2)];
			//camera.x = 0;
//			camera.y = 100;
//			camera.z = 0;
			//camera.moveBackward(600);
			
			//camera = new InteractiveCamera3D(viewport);
			//camera.z = -700;
			viewport.opaqueBackground = 0x000000;
			 //demo
			// leftViewport = new Viewport3D(300,300);
//			  leftViewport.x=400;
//			  leftViewport.opaqueBackground = 0x000000;
//			  leftViewport.filters = [new GlowFilter(0xcc0000, 1, 10, 10, 2)];
//			 addChild(leftViewport);
			
			
			//centerCamera = new InteractiveCamera3D(mainViewport);
			
			scene= new Scene3D();
			sceneMain= new Scene3D();
			renderer = new BasicRenderEngine();
			
			
//set XML
			
			//stagex=stage.stageWidth;
			//stagey=stage.stageHeight;
			trace(stagey);
			//them vao 3 button va cong them 1 background
			var sprite: Sprite = new Sprite();
			sprite.y=2;
			sprite.x=13;
			var sprite2: Sprite = new Sprite();
			sprite2.y=205;
			sprite2.x=13;
			addChild(sprite);
			addChild(sprite2);
			var bg:Shape=drawObject(600,240,"0xA6A6A6","0xffffff");
			var bg2:Shape=drawObject(600,465,"0xA6A6A6","0xffffff");
			//set bg for bg
			var newShape=new Shape();
			var colours=[0xff0000,0xcc0000];
			var alphas=[1,1];
			var ratios=[0,255];
			var radians:Number=90*(Math.PI/180);
			var matrix:Matrix=new Matrix();
			matrix.createGradientBox(150,50,radians);
			newShape.graphics.beginGradientFill(GradientType.LINEAR,colours,alphas,ratios,matrix);
			newShape.graphics.drawRoundRect(0,0,155/1.5,50/1.2,5,5);
			newShape.y=5;
			///
			var newShape2=new Shape();
			var colours2=[0xff0000,0xcc0000];
			var alphas2=[1,1];
			var ratios2=[0,255];
			var radians2:Number=90*(Math.PI/180);
			var matrix2:Matrix=new Matrix();
			matrix2.createGradientBox(150,50,radians);
			newShape2.graphics.beginGradientFill(GradientType.LINEAR,colours,alphas,ratios,matrix);
			newShape2.graphics.drawRoundRect(0,0,155/1.5,50/1.2,5,5);
			newShape2.y=5;
			
			
			sprite.addChild(bg);
			sprite.addChild(newShape);
			sprite2.addChild(bg2);
			sprite2.addChild(newShape2);
			
			sprite.addChild(d1);
			sprite.addChild(d2);
			sprite.addChild(d3);
			
			doc.y=32;
			//main custom
			bg3=drawObject(1150,680,"0xff9900","0xff3300");
			bg3.x=415;
			bg3.y=30;
			//add child customvp
			addChild(_customvp);
			
			//set viewport cus container
			bgCustom = drawObject(1150,680,"0xff9933","0xff9966");
			bgCustom.x=415;
			bgCustom.y=30;
			bgCustom.alpha=0;
			_customvp.addChild(bgCustom);
			addChild(containerM);
			//containerM.transform.matrix= new Matrix(1,0,0,1,-415,-50);
			
			addChild(doc);
			//SET documet for 2
			doc2.y=235;
			addChild(doc2);
			
			//set _container for bitmapviewport
			_container.x=0;
			_container.y=0;
			addChild(_container);
			d1.x=10;
			 var effect1: GlowFilter= new GlowFilter(0xff0000, 1, 10, 10, .5);
			 effect1.alpha=1;
			//d1.filters=[effect1];
			d2.x=140;
			d1.y=d2.y=d3.y=50;		
			d3.x=270;
			d4.x=1050;
			d4.y=50;
			d3.rolL();
			d1.addEventListener(DropDownMenuEvent.CHANGE,d1Change);
			d2.addEventListener(DropDownMenuEvent.CHANGE,d2Change);
			d3.addEventListener(DropDownMenuEvent.CHANGE,d3Change);
			d4.addEventListener(DropDownMenuEvent.CHANGE,d4Change);
			
			ms.open();
			addChild(ms);	
		 stage.align = StageAlign.TOP_LEFT;
         stage.scaleMode = StageScaleMode.NO_SCALE;
         //ap dung cho ca stage****************graphics.beginFill(0x000000, 1);
		// graphics.lineStyle(1, 0xff0000);
//         graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
//         graphics.endFill();
		// var effect1: GlowFilter= new GlowFilter(0xcc0000, 1, 10, 10, 2);
		// this.filters=[effect1];
			
		}
		public function mainOut(evt:MouseEvent):void{
			trace("mouseOut bg3");
			
		}	
		public function mainOver(evt:MouseEvent):void{
			trace("mouseover bg3");
		}	
		private function d1Change(e:DropDownMenuEvent):void{

			d2.load(e.value);
			ms._txt="Now, You must click in the SELECT FIRM for selecting a car's Firm";
			ms.open();
		}	
		private function d2Change(e:DropDownMenuEvent):void{
	
			ms._txt="Let click in the SELECT NAME for selecting a car' name. And Customized...";
			ms.open();
			d3.load(e.value);
		}
		private function d4Change(e:DropDownMenuEvent):void{
			//trace(e.prototype);
			//customVp.

			customVp.loadDAE(e.value);
			trace("gia tri custom");
		}
		
		
		private function d3Change(e:DropDownMenuEvent):void{
			//custom car
			if(e.value==null)
				return;
			d4.load(e.prototype);
			trace(e.caption+" = "+e.value);
			// xoa het cac con trong document_page
			var total:int=_container.numChildren;
			for(var i:int=0;i<total;i++){ _container.removeChildAt(0); }
			Document_page= new BitmapViewport(e.value) ;
			addChild(Document_page);
			Document_page.x=-150;
			Document_page.y=220;
			//set bo nho su dung
			trace('Memory added for sphere: ' + (System.totalMemory - memoryBefore).toString());
//			//set menu documentPage va control button
			materialButton = new PushButton(this, 20, 260, "Wireframe",onclick);
			customButton = new PushButton(this, 20, 290, "Custom Color",onClick);
			noteButton = new PushButton(this, 20, 320, "Default Color",onclick2);
			addChildModel= new PushButton(this, 20, 350, "Customized",addChildCar);
			help= new PushButton(this, 20, 380, "Help",onclick2);
     
		}
		//set onclick for custom menu
		private function onClick(evt:Event):void{
				//Document_page.remove();
				Document_page.changeColor();
			//mainViewport.opaqueBackground = 0xffffff;
			//addChild(mainViewport);
				//on load for model option
				//var onLoad:
			}
		private function onclick2(evt:Event):void{
				//Document_page.remove();
				Document_page.orgModel();
				Document_page.unWrapMap();
				
			//mainViewport.opaqueBackground = 0xffffff;
			//addChild(mainViewport);
				//on load for model option
				//var onLoad:
			}
		private function onclick(evt:Event):void{
				//Document_page.remove();
				
				Document_page.orgModel();
			//mainViewport.opaqueBackground = 0xffffff;
			//addChild(mainViewport);
				//on load for model option
				//var onLoad:
			}
		private function addChildCar(evt:Event):void{
				Document_page.removeEvent();
				//set for text Warnning
				txtFormat.color=0x000000;
				txtFormat.size=11;
				txtFormat.font="Times New Roman";
				txtFormat.align = TextFormatAlign.CENTER;
				txtFormat.bold=true;
				txt.defaultTextFormat=txtFormat;			
				txt.mouseEnabled=false;
				txt.text=" Customized";
				txt.width=600;
				txt.height=50;
				addChild(txt);
				txt.x= 500;
				txt.y=5;
				//txt.border=true;
//				txt.borderColor=0xffffff;
				txt.wordWrap = true;
					
				trace(containerM.numChildren);
				var total:int=containerM.numChildren;
				for(var i:int=0;i<total;i++){ containerM.removeChildAt(0); }
				//
				containerM.addChild(bg3);
				containerM.addEventListener(MouseEvent.MOUSE_OVER,mainOver);
				containerM.addEventListener(MouseEvent.MOUSE_OUT,mainOut);
			
				mainCustom = new Papervision("xml/xe.dae");
				mainCustom.x=420;
				mainCustom.y=100;
				containerM.addChild(mainCustom);
				//set for label 
				
				
				//set for menu main customzied
				topLabel = new Label(containerM,450,30,"MOTION EFFECTS");
				rotationButton = new PushButton(containerM, 550, 50, "Rotation",OriginalMain);
				zoomButton = new PushButton(containerM, 850, 50, "Zoom",zoomMain);
				note2Button = new PushButton(this, 750, 50, "Pick Color",noteMain);
				custom2Button= new PushButton(containerM, 950, 50, "Mount",customMain);
				//set for down buuton
				topLabel = new Label(containerM,450,550,"CUSTOMIZED");
				//fWheelButton = new PushButton(containerM, 500, 570, "Demount",fWheelMain);
				//bWheelButton = new PushButton(containerM, 600, 570, "Mount",bWheelMain);
				//rMirrorsButton = new PushButton(containerM, 700, 570, "Auto Mount", rMirrorsMain);
				accessoryButton= new PushButton(this, 650, 50, "Disable Help",accessoryMain);
				//bodyButton= new PushButton(containerM, 900, 570, "Body",bodyMain);
				//fWheelButton = new PushButton(containerM, 500, 570, "Front Wheels",fWheelMain);
//				bWheelButton = new PushButton(containerM, 600, 570, "Back Wheels",bWheelMain);
//				rMirrorsButton = new PushButton(containerM, 700, 570, "Rearview MirrorS", rMirrorsMain);
//				accessoryButton= new PushButton(containerM, 800, 570, "Accessory",accessoryMain);
//				bodyButton= new PushButton(containerM, 900, 570, "Body",bodyMain);
				//set for 2 button cancel and ok
				cancelButton= new PushButton(_customvp, 650, 500, "Cancel",cancelMain);
				okButton= new PushButton(_customvp, 750, 500, "Ok",okCustomVp);
					
		}
		//SET DOWN BUUTON
		public static function cEffect(event:Event=null):void{
				TweenLite.to(containerM,1.5,{x:0,scaleX:1,alpha:1});
				TweenLite.to(_customvp,.5,{alpha:0});
			}
		private function okCustomVp(event:Event):void{
				cEffect();
				mainCustom.loadDAEcustom();
				
				
			}
		private function cancelMain(event:Event):void{
				cEffect();
				mainCustom.cancelDAE();
			}
		private function fWheelMain(event:Event):void{
				
			}
		private function bWheelMain(event:Event):void{
				
			}
		private function bodyMain(event:Event):void{
			
			}
		private function rMirrorsMain(event:Event):void{
				
			}
		private function accessoryMain(event:Event):void{
				if(event.target.label=="Disable Help"){
					event.target.label="Enable Help";
					removeChild(txt);
				}
				else
				{
					event.target.label="Disable Help";
					addChild(txt);
				}
			}
			
		public static function pEffect(event:Event=null):void{
			if(_customvp.numChildren >3){
						customVp.killDAE();
						customVp.loadDAE("xml/xe.dae");
						TweenLite.to(_customvp,.5,{alpha:1});
						TweenLite.to(containerM,.1,{x:bg3.x,scaleX:0,alpha:0});
						
						}
			else if(_customvp.numChildren==3){
				trace("vong lap chay qua day");
						bgCustom.alpha=1;
						_customvp.addChild(d4);
						customVp.x=480;
						customVp.y=100;
						_customvp.addChild(customVp);
						
						customVp.loadDAE("xml/xe.dae");
						//customLoadDAE();
						TweenLite.to(containerM,.1,{x:bg3.x,scaleX:0,alpha:0});
						TweenLite.to(_customvp,.5,{alpha:1});
				}
		}
		public function customLoadDAE():void{
	//code
		}
		private function OriginalMain(event:Event):void{
			
				txt.text="U must mouse drag on stage for rotating object";
				mainCustom.detroyEvent("1");
				mainCustom.rotationDAE();
			}
		private function zoomMain(event:Event):void{
				txt.text="Let press Ctrl+ mousedrag for zoom object ";
				mainCustom.detroyEvent("2");
				mainCustom.OriginalState();
			}
		private function noteMain(event:Event):void{
				txt.text="pick one color for fill in the DisplayObject";
				if(containerM.scaleX==0)
					customVp.changeColor();
				else 
					mainCustom.changeColor();
			}
		private function customMain(event:Event):void{
				//mainCustom.detroyEvent("1");
				txt.text="click on part in object and drag on left or right for customzied";
				mainCustom.detroyEvent("2");
				mainCustom.selectEvent();
			}

		
	}
}
 
  